<template>
  <div class="container">
    <div class="app-container">
      <el-button class="btn-add" size="mini" type="primary" @click="postFistPermission">添加权限</el-button>
      <el-table default-expand-all :data="list" row-key="id">
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="code" label="标识" />
        <el-table-column prop="description" label="描述" />
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button v-if="row.type === 1" size="mini" type="text" @click="postSecondPermission(row)">添加</el-button>
            <el-button size="mini" type="text" @click="editPermission(row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="delPermission(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 新增权限点 -->
    <el-dialog title="新增权限点" :visible.sync="dialogFormVisible" label-width="120px">
      <el-form ref="refForm" :model="form" :rules="rules">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="form.name" autocomplete="off" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="form.code" autocomplete="off" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="form.description" autocomplete="off" style="width:90%" />
        </el-form-item>
        <el-form-item label="开启">
          <el-switch
            v-model="form.enVisible"
            style="margin-right: 20px;"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" type="primary" @click="addBtnOK">确定</el-button>
          <el-button size="small" @click="dialogFormVisible = false">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
    <!-- 编辑权限点 -->
    <el-dialog title="编辑权限点" :visible.sync="dialogEditFormVisible" label-width="120px">
      <el-form ref="refEditForm" :model="editForm" :rules="rules">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="editForm.name" autocomplete="off" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="editForm.code" autocomplete="off" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="editForm.description" autocomplete="off" style="width:90%" />
        </el-form-item>
        <el-form-item label="开启">
          <el-switch
            v-model="editForm.enVisible"
            style="margin-right: 20px;"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" type="primary" @click="editBtnOk">确定</el-button>
          <el-button size="small" @click="dialogEditFormVisible = false">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import { getPermissionList, delPermission, postPermission, getPermission, putPermission } from '@/api/permission.js'
import { transListToTreeData } from '@/utils/index.js'
export default {
  name: 'PermissionPage',
  data() {
    return {
      list: [],
      dialogFormVisible: false,
      dialogEditFormVisible: false,
      form: {
        name: '',
        code: '',
        description: '',
        type: '',
        pid: '',
        enVisble: ''
      },
      editForm: {
        name: '',
        code: '',
        description: '',
        type: '',
        pid: '',
        enVisble: ''
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '权限描述不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    // 编辑弹框确认按钮
    async editBtnOk() {
      await this.$refs.refEditForm.validate()
      console.log('校验通过')
      await putPermission(this.editForm)
      this.$message.success('编辑权限成功')
      await this.getPermissionList()
      this.dialogEditFormVisible = false
    },
    // 编辑权限点
    async editPermission(id) {
      await this.$refs.refEditForm?.resetFields()
      this.dialogEditFormVisible = true
      this.editForm = await getPermission(id)
    },
    // 添加弹框确认按钮
    async addBtnOK() {
      await this.$refs.refForm.validate()
      console.log('校验通过')
      await postPermission(this.form)
      this.$message.success('添加权限成功')
      await this.getPermissionList()
      this.dialogFormVisible = false
    },
    // 新增二级权限操作
    async postSecondPermission(row) {
      console.log(row, 'row')
      this.$refs.refForm?.resetFields()
      this.dialogFormVisible = true
      this.form.type = 2
      this.form.pid = row.id
    },
    // 新增一级权限操作
    postFistPermission() {
      this.$refs.refForm?.resetFields()
      this.dialogFormVisible = true
      this.form.type = 1
      this.form.pid = 0
    },
    // 删除操作
    async delPermission(id) {
      try {
        await this.$confirm('确定要删除该数据吗')
        await delPermission(id)
        this.getPermissionList()
        this.$message.success('删除成功')
      } catch (error) {
        console.log(error, '删除err')
      }
    },
    async getPermissionList() {
      // 将列表数据转化成树形结构
      this.list = transListToTreeData(await getPermissionList(), 0)
    }
  }
}
</script>
<style>
.btn-add {
  margin: 10px;
}
</style>
